#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <omp.h>

#include "array.h"
#include "sortingAlgorithm.h"
#include "bubbleSort.h"
#include "shearSort.h"
#include "bitonicSort.h"
#include "evenOddMergeSort.h"

using namespace std;

double diffclock(clock_t clock1,clock_t clock2);

int main(int argc, char * argv[])
{
    if (argc != 3)
    {
        cout << "Wrong number of arguments. Expected 3 but given " << argc << endl;
        cout << "Use: SORT_ALG_NAME INPUT_FILE, for example: bubble array10.txt" << endl;
        cout << "SORT_ALG_NAME = {'bubble', 'shear', 'bitonic'}" << endl;
        exit(1);
    }

    Array * a = new Array(argv[2]);
    SortingAlgorithm * sa;

    if(strcmp("bubble", argv[1]) == 0){
        sa = new BubbleSort();
    }else if(strcmp("shear", argv[1]) == 0){
        sa = new ShearSort();
    }else if(strcmp("bitonic", argv[1]) == 0){
        sa = new BitonicSort();
    } else if (strcmp("evenodd", argv[1]) == 0) {
        sa = new EvenOddMergeSort();
    }else{
        cout << "Wrong argument." << endl;
        cout << "Use: SORT_ALG_NAME INPUT_FILE, for example: bubble array10.txt" << endl;
        cout << "SORT_ALG_NAME = {'bubble', 'shear', 'bitonic'}" << endl;
        exit(1);
    }

    cout << "------ Unsorted Array ------" << endl;
    //a->printArray();

    double begin = omp_get_wtime();

    sa->sort(a);

    double end = omp_get_wtime();

    cout << "------ Sorted Array ------" << endl;
    //a->printArray();

    cout << "Time elapsed: " << setprecision(6) << fixed << (end - begin) << " s." << endl;

    delete a;
    delete sa;

    return 0;
}

double diffclock(clock_t clock1, clock_t clock2)
{
        double diffticks=clock1-clock2;
        double diffms=(diffticks*1000)/CLOCKS_PER_SEC;
        return diffms;
}
